package com.xl.runner;

import com.xl.domain.entity.Article;
import com.xl.service.ArticleService;
import com.xl.utils.RedisCache;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ViewCountRunner implements CommandLineRunner {

    @Resource
    private ArticleService articleService;
    @Resource
    private RedisCache redisCache;


    @Override
    public void run(String... args) throws Exception {
        //查询博客信息 id ViewCount
        List<Article> articles = articleService.list(null);
        Map<String, Integer> viewCountMap = articles.stream()
                .collect(Collectors.toMap(article -> article.getId().toString(), article -> article.getViewCount().intValue()));
        //存储到redis
        redisCache.setCacheMap("article:viewCount",viewCountMap);

    }
}
